<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
            <a-form-item label="品番编码">
              <a-input v-model="queryParam.skuCode" placeholder="请输入品番编码"></a-input>
            </a-form-item>
          </a-col>
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
            <a-form-item label="品番名称">
              <a-input v-model="queryParam.skuName" placeholder="请输入品番名称"></a-input>
            </a-form-item>
          </a-col>
          <template v-if="toggleSearchStatus">
            <a-col :lg="7" :md="8" :sm="24" :xl="6">
              <a-form-item label="所属库区">
                <a-input v-model="queryParam.area" placeholder="请输入所属库区"></a-input>
              </a-form-item>
            </a-col>
            <a-col :lg="7" :md="8" :sm="24" :xl="6">
              <a-form-item label="货架位置">
                <a-input v-model="queryParam.location" placeholder="请输入货架位置"></a-input>
              </a-form-item>
            </a-col>
            <a-col :lg="7" :md="8" :sm="24" :xl="6">
              <a-form-item label="库存数量">
                <a-input v-model="queryParam.number" placeholder="请输入库存数量"></a-input>
              </a-form-item>
            </a-col>
            <a-col :lg="7" :md="8" :sm="24" :xl="6">
              <a-form-item label="盘点数量">
                <a-input v-model="queryParam.stocktakeNum" placeholder="请输入盘点数量"></a-input>
              </a-form-item>
            </a-col>
            <a-col :lg="7" :md="8" :sm="24" :xl="6">
              <a-form-item label="相差数量">
                <a-input v-model="queryParam.disparityNum" placeholder="请输入相差数量"></a-input>
              </a-form-item>
            </a-col>
            <a-col :lg="7" :md="8" :sm="24" :xl="6">
              <a-form-item label="差异原因">
                <a-input v-model="queryParam.remarks" placeholder="请输入差异原因"></a-input>
              </a-form-item>
            </a-col>
            <a-col :lg="11" :md="12" :sm="24" :xl="10">
              <a-form-item label="盘点日期">
                <j-date v-model="queryParam.stocktakeDate_begin" class="query-group-cust"
                        placeholder="请选择开始日期"></j-date>
                <span class="query-group-split-cust"></span>
                <j-date v-model="queryParam.stocktakeDate_end" class="query-group-cust"
                        placeholder="请选择结束日期"></j-date>
              </a-form-item>
            </a-col>
            <a-col :lg="11" :md="12" :sm="24" :xl="10">
              <a-form-item label="确认日期">
                <j-date v-model="queryParam.confirmDate_begin" class="query-group-cust"
                        placeholder="请选择开始日期"></j-date>
                <span class="query-group-split-cust"></span>
                <j-date v-model="queryParam.confirmDate_end" class="query-group-cust"
                        placeholder="请选择结束日期"></j-date>
              </a-form-item>
            </a-col>
            <a-col :lg="7" :md="8" :sm="24" :xl="6">
              <a-form-item label="操作人">
                <a-input v-model="queryParam.operateUser" placeholder="请输入操作人"></a-input>
              </a-form-item>
            </a-col>
            <a-col :lg="7" :md="8" :sm="24" :xl="6">
              <a-form-item label="确认人">
                <a-input v-model="queryParam.confirmUser" placeholder="请输入确认人"></a-input>
              </a-form-item>
            </a-col>
          </template>
          <a-col :lg="7" :md="8" :sm="24" :xl="6">
            <span class="table-page-search-submitButtons" style="float: left;overflow: hidden;">
              <a-button icon="search" type="primary" @click="searchQuery">查询</a-button>
              <a-button icon="reload" style="margin-left: 8px" type="primary" @click="searchReset">重置</a-button>
              <a style="margin-left: 8px" @click="handleToggleSearch">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button icon="plus" type="primary" @click="handleAdd">新增</a-button>
      <a-button icon="download" type="primary" @click="handleExportXls('盘点')">导出</a-button>
      <a-upload :action="importExcelUrl" :headers="tokenHeader" :multiple="false" :showUploadList="false" name="file"
                @change="handleImportExcel">
        <a-button icon="import" type="primary">导入</a-button>
      </a-upload>
      <!-- 高级查询区域 -->
      <!--      <j-super-query ref="superQueryModal" :fieldList="superFieldList"-->
      <!--                     @handleSuperQuery="handleSuperQuery"></j-super-query>-->
    </div>

    <a-form layout="inline">
      <a-row :gutter="24">
        <a-col :lg="7" :md="8" :sm="24" :xl="6">
          <a-form-item label="工作站选择">
            <select id="station-select" v-model="selectedStations"
                    class="select-box">
              <option disabled selected value="">请选择工作站</option>
              <option v-for="(station, index) in stations" :key="index"
                      :value="station">
                {{ station }}
              </option>
            </select>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>
    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a
        style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        :columns="columns"
        :dataSource="dataSource"
        :loading="loading"
        :pagination="ipagination"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        :scroll="{x:true}"
        bordered
        class="j-table-force-nowrap"
        rowKey="id"
        size="middle"
        @change="handleTableChange">
        <template slot="stocktakeNum" slot-scope="text, record">
          <a-input v-if="record.status == 0" v-model="record.stocktakeNum" placeholder="请输入盘点数量"
                   style="width: 90%;" type="number" />
          <a v-if="record.status == 0" @click="confirmStocktakingNumber(record)">确认</a>
          <div v-else v-html="text"></div>
        </template>
        <template slot="status" slot-scope="text">
          <div v-if="text==0">新建</div>
          <div v-if="text==1">盘盈</div>
          <div v-if="text==2">盘亏</div>
          <div v-if="text==3">盘盈-已完成</div>
          <div v-if="text==4">盘亏-已完成</div>
          <div v-if="text==5">盘平-已完成</div>
        </template>
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" alt="" height="25px"
               style="max-width:80px;font-size: 12px;font-style: italic;" />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            icon="download"
            size="small"
            type="primary"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a v-if="record.status==0" @click="handleEdit(record)">编辑</a>
          <a-divider v-if="record.status==0" type="vertical" />
          <a v-if="record.status==0&&!record.stationCode&&record.area=='A'" @click="callShelf(record)">呼叫货架</a>
          <a-divider v-if="record.status==0&&!record.stationCode&&record.area=='A'" type="vertical" />
          <a v-if="record.status==1||record.status==2" @click="sycn(record)">处理差异</a>
          <a-divider v-if="record.status==1||record.status==2" type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a @click="handleDetail(record)">详情</a>
              </a-menu-item>
              <a-menu-item v-if="record.status==0">
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>

      </a-table>
    </div>

    <stocktake-modal ref="modalForm" @ok="modalFormOk"></stocktake-modal>
    <a-modal :confirm-loading="sycnLoading" :visible="showSycn" title="差异原因" @cancel="sycnCancel"
             @ok="sycnOk">
      <a-textarea v-model="reason" :auto-size="{ minRows: 3, maxRows: 5 }"
                  placeholder="请输入差异原因" />
    </a-modal>
  </a-card>
</template>

<script>

import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import StocktakeModal from './modules/StocktakeModal'
import { getAction, postAction } from '@api/manage'

export default {
  name: 'StocktakeList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    StocktakeModal
  },
  data() {
    return {
      isorter: {
        column: 'status',
        order: 'asc'
      },
      description: '盘点管理页面',
      selectedStations: '', // 当前选中的工作站
      stations: [],// 工作站列表
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '品番编码',
          align: 'center',
          dataIndex: 'skuCode'
        },
        {
          title: '品番名称',
          align: 'center',
          dataIndex: 'skuName'
        },
        {
          title: '所属库区',
          align: 'center',
          dataIndex: 'area_dictText'
        },
        {
          title: '货架位置',
          align: 'center',
          dataIndex: 'location'
        },
        {
          title: '库存数量',
          align: 'center',
          dataIndex: 'number'
        },
        {
          title: '盘点数量',
          align: 'center',
          dataIndex: 'stocktakeNum',
          scopedSlots: { customRender: 'stocktakeNum' }
        },
        {
          title: '相差数量',
          align: 'center',
          dataIndex: 'disparityNum'
        },
        {
          title: '差异原因',
          align: 'center',
          dataIndex: 'remarks'
        },
        {
          title: '状态',
          align: 'center',
          dataIndex: 'status',
          scopedSlots: { customRender: 'status' }
        },
        {
          title: '盘点日期',
          align: 'center',
          dataIndex: 'stocktakeDate',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '操作人',
          align: 'center',
          dataIndex: 'operateUser'
        },
        {
          title: '确认日期',
          align: 'center',
          dataIndex: 'confirmDate',
          customRender: function(text) {
            return !text ? '' : (text.length > 10 ? text.substr(0, 10) : text)
          }
        },
        {
          title: '确认人',
          align: 'center',
          dataIndex: 'confirmUser'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' }
        }
      ],
      url: {
        list: '/stocktake/stocktake/list',
        delete: '/stocktake/stocktake/delete',
        deleteBatch: '/stocktake/stocktake/deleteBatch',
        exportXlsUrl: '/stocktake/stocktake/exportXls',
        importExcelUrl: 'stocktake/stocktake/importExcel',
        confirmStocktakingNumber: 'stocktake/stocktake/confirmStocktakingNumber',
        sycn: 'stocktake/stocktake/sycn',
        callShelfEdit: 'stocktake/stocktake/callShelfEdit',
        callShelf: '/inOrder/inOrder/callShelf',
        release: '/inOrder/inOrder/release/',//货架放行
        getStations: '/basicStationInfo/basicStationInfo/list',//查询工作站
        getShelf: 'stocktake/stocktake/getShelf'
      },
      showSycn: false,
      checkInventory: undefined,
      reason: undefined,
      sycnLoading: false,
      dictOptions: {},
      superFieldList: []
    }
  },
  created() {
    this.getSuperFieldList()
    this.getStations()
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    getStations() {
      let that = this
      getAction(this.url.getStations).then(res => {
        const list = res.result.records.map(item => {
          return item.stationName
        })
        that.stations = list
      }).catch((error) => {
        console.log(error)
      })
    },
    release(data) {//货架放行
      if (data.stationCode) {
        getAction(this.url.release + data.location + '/' + data.stationCode).then((res) => {
          if (res.code === 200) {
            this.$message.success(res.message)
          } else {
            this.$message.warning(res.message)
          }
        }).catch((error) => {
          this.$message.warning(res.message)
        })
      }
    },
    //呼叫货架点击事件
    callShelf(data) {
      if (!this.selectedStations) {
        this.$message.warning('请先选择工作站')
        return // 结束方法，不继续执行后续逻辑
      }
      if (!data.location) {
        this.$message.warning('请选择呼叫的货架')
        return // 结束方法，不继续执行后续逻辑
      }
      data.stationCode = this.selectedStations
      postAction(this.url.getShelf, data).then((res2) => {
        postAction(this.url.callShelf, { station: this.selectedStations, shelfIds: res2.result }).then((res1) => {
          postAction(this.url.callShelfEdit, data).then((res) => {
            this.loadData()
            this.$message.success(res1.message)
          })
        })
      }).catch((error) => {
        this.$message.warning(res.message)
      })

    },
    sycnOk(e) {
      if (!this.reason) {
        this.$message.error('请输入原因')
        return
      }
      this.checkInventory.remarks = this.reason
      this.sycnLoading = true
      postAction(this.url.sycn, this.checkInventory).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      }).finally(() => {
        this.showSycn = false
        this.sycnLoading = false
        this.reason = undefined
        this.checkInventory = undefined
      })
    },
    sycnCancel(e) {
      this.showSycn = false
    },
    sycn(data) {
      this.showSycn = true
      this.checkInventory = data
    },
    confirmStocktakingNumber(data) {
      if (!data.stocktakeNum) {
        this.$message.warning('请输入盘点确认数量')
        return
      }
      postAction(this.url.confirmStocktakingNumber, data).then((res) => {
        if (res.success) {
          this.$message.success(res.message)
          this.release(data)
          this.loadData()
        } else {
          this.$message.warning(res.message)
        }
      })
    },
    initDictConfig() {
    },
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'skuCode', text: '品番编码', dictCode: '' })
      fieldList.push({ type: 'string', value: 'skuName', text: '品番名称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'area', text: '所属库区', dictCode: '' })
      fieldList.push({ type: 'string', value: 'location', text: '货架位置', dictCode: '' })
      fieldList.push({ type: 'int', value: 'number', text: '库存数量', dictCode: '' })
      fieldList.push({ type: 'int', value: 'stocktakeNum', text: '盘点数量', dictCode: '' })
      fieldList.push({ type: 'int', value: 'disparityNum', text: '相差数量', dictCode: '' })
      fieldList.push({ type: 'string', value: 'remarks', text: '差异原因', dictCode: '' })
      fieldList.push({ type: 'string', value: 'status', text: '状态', dictCode: '' })
      fieldList.push({ type: 'date', value: 'stocktakeDate', text: '盘点日期' })
      fieldList.push({ type: 'date', value: 'confirmDate', text: '确认日期' })
      fieldList.push({ type: 'string', value: 'operateUser', text: '操作人', dictCode: '' })
      fieldList.push({ type: 'string', value: 'confirmUser', text: '确认人', dictCode: '' })
      this.superFieldList = fieldList
    }
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>